package pingpongdojo.auth;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.ui.AbstractSourceProvider;
import org.eclipse.ui.ISources;

public class SessionSourceProvider extends AbstractSourceProvider { 
    public final static String SESSION_STATE = "pingpongdojo.auth.sessionState";

    private final static String LOGGED_IN = "loggedIn"; 
    private final static String LOGGED_OUT = "loggedOut"; 
    String currentState = LOGGED_OUT; 
    boolean loggedIn; 

    @Override 
    public String[] getProvidedSourceNames() { 
        return new String[] {SESSION_STATE}; 
    } 
    @Override 
    public Map<String, String> getCurrentState() { 
        Map<String, String> currentStates = new HashMap<String, String>(1); 
        currentState =  loggedIn?LOGGED_IN:LOGGED_OUT; 
        currentStates.put(SESSION_STATE, currentState);
            	
        System.err.println("Get current state was called!: " +  currentState);
        return currentStates; 
    } 
    
    public boolean isLoggedIn(){
    	return loggedIn;
    } 
     @Override 
    public void dispose() {} 
     
    public void setLoggedIn(boolean loggedIn) { 
    	System.err.println("seLoggedIn: " + loggedIn);
        if(this.loggedIn == loggedIn)  {
        	System.err.println("No change");
            return; // no change    
        }   
        this.loggedIn = loggedIn;
        
        fireSourceChanged(ISources.WORKBENCH, getCurrentState());
  //      fireSourceChanged(ISources.WORKBENCH, SESSION_STATE, currentState); 
    } 
}